package com.contactsconverter.adapter;

import java.util.Date;
import java.util.List;

import com.contactsconverter.R;


import com.difsonapps.contacttranslator.dto.EventDTO;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CTEventAdapter extends ArrayAdapter<EventDTO>{

	private Context mContext;
	private List<EventDTO> mListOffEvent;

	public CTEventAdapter(Context context, int resource,
			List<EventDTO> objects) {
		super(context, resource, objects);
		this.mContext = context;
		this.mListOffEvent = objects;
	}


	@Override
	public View getDropDownView(int position, View convertView,ViewGroup parent) {
		return getView(position, convertView, parent);
	}



	@Override
	public int getPosition(EventDTO item) {
		int n = 0;
		for (EventDTO event : mListOffEvent) {
			if(event.getId() == item.getId())
				return n;
			n++;
		}
		return super.getPosition(item);
	}
	/*@Override
	public  View getView(int position, View convertView, ViewGroup parent)  {

		final CTContactModel contact = mListOffContact.get(position);

		LayoutInflater mInflater = (LayoutInflater)
				mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {			
			convertView = mInflater.inflate(R.layout.contact_row, null);
		}
		TextView txtName = (TextView) convertView.findViewById(R.id.tn_contact_name);
		txtName.setText(contact.getName());
		( (CheckBox ) convertView.findViewById(R.id.tn_contact_check)).setChecked(contact.isSelected());
		ImageView imgIcon = (ImageView) convertView.findViewById(R.id.tn_contact_user_avatar);
		imgIcon.setImageBitmap(contact.getAvatar());
		TextView ctPhoneNumber = (TextView) convertView.findViewById(R.id.ct_phone_number);
		ctPhoneNumber.setText(contact.toString2());
		

		return convertView;
	}*/

	@Override
	public  View getView(int position, View convertView, ViewGroup parent)  {

		 final EventDTO event = mListOffEvent.get(position);
		 
		LayoutInflater mInflater = (LayoutInflater)
				mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {			
			convertView = mInflater.inflate(R.layout.event_row, null);
		}
		
		convertView.setFocusable(false);
		TextView txtTitle = (TextView) convertView.findViewById(R.id.et_event_title);
		txtTitle.setText(event.getTitle());
		
		TextView txtTime = (TextView) convertView.findViewById(R.id.et_event_time);
		txtTime.setText(new Date().toGMTString());
		
		
		TextView txtContent = (TextView) convertView.findViewById(R.id.et_event_content);
		txtContent.setText(Html.fromHtml(event.getContent()));
		
	
		
		ImageView imgIcon = (ImageView) convertView.findViewById(R.id.et_event_icon);
		imgIcon.setImageDrawable(this.getContext().getResources().getDrawable(R.drawable.event));
		
		
		//TextView ctPhoneNumber = (TextView) convertView.findViewById(R.id.ct_phone_number);
		//ctPhoneNumber.setText(contact.toString2());
		
		
		return convertView;
	}

}
